<template>
  <a-layout>

    <a-card :bordered="false">
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="hanldleAdd()" :disabled="isAddBtn">新建</a-button>
        <a-button
          type="primary"
          icon="minus"
          @click="handleDelete(selectedRowKeys)"
          :disabled="!hasSelected()"
          :loading="loading"
        >删除</a-button>
      </div>

      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="班次名称">
                <a-input v-model="queryParam.ParentWorkingTimesName" placeholder :disabled="true"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="getDataList">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table
        ref="table"
        :columns="columns"
        :rowKey="row => row.WorkingTimesID"
        :dataSource="data"
        :pagination="pagination"
        :loading="loading"
        @change="handleTableChange"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :bordered="true"
        size="small"
      >
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record.WorkingTimesID)">编辑</a>
            <a-divider type="vertical" />
            <a @click="handleDelete([record.WorkingTimesID])">删除</a>
          </template>
        </span>
      </a-table>

      <edit-form ref="editForm" :row-data="rowData" :afterSubmit="getDataList"></edit-form>
    </a-card>
  </a-layout>
</template>

<script>
import EditForm from './DetailEditForm'
const columns = [
  { title: '班次名称', dataIndex: 'WorkingTimesName' },
  { title: '开始时间', dataIndex: 'StartTime', width: '10%' },
  { title: '结束时间', dataIndex: 'EndTime', width: '10%' },
  { title: '工作时长', dataIndex: 'WorkHour', width: '10%' },
  { title: '休息时长', dataIndex: 'RestHour', width: '10%' },
  { title: '类型', dataIndex: 'WorkingType', width: '10%' },
  { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' }, width: '100px' }
]

export default {
  components: {
    EditForm
  },
  props: {
    // eslint-disable-next-line vue/require-default-prop
    rowData: {
      type: Object
    }
  },
  watch: {
    rowData () {
      this.temp = this.rowData
    }
  },
  mounted () {
    if (this.rowData.WorkingTimesID !== undefined) {
      this.isAddBtn = false
      this.queryParam.ParentWorkingTimesID = this.rowData.WorkingTimesID
      this.queryParam.ParentWorkingTimesName = this.rowData.WorkingTimesName
      this.getDataList()
    }
  },
  data () {
    return {
      data: [],
      isAddBtn: true,
      temp: this.rowData,
      pagination: {
        current: 1,
        pageSize: 10,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`
      },
      filters: {},
      sorter: { field: 'WorkingTimesID', order: 'asc' },
      loading: false,
      columns,
      queryParam: {},
      visible: false,
      selectedRowKeys: []
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/Aps/Aps_WorkingTimes/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'WorkingTimesID',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd () {
      this.rowData = this.rowData
      this.$refs.editForm.openForm()
    },
    handleEdit (id) {
      this.rowData = this.rowData
      this.$refs.editForm.openForm(id)
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.submitDelete(ids, resolve, reject)
          }).catch(() => console.log('Oops errors!'))
        }
      })
    },
    submitDelete (ids, resolve, reject) {
      this.$http.post('/Aps/Aps_WorkingTimes/DeleteData', ids).then(resJson => {
        resolve()

        if (resJson.Success) {
          this.$message.success('操作成功!')

          this.getDataList()
        } else {
          this.$message.error(resJson.Msg)
        }
      })
    }
  }
}
</script>
